package com.cmti.skeleton.dao.mapper;

import com.cmti.skeleton.entity.SysDict;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

public interface SysDictMapper {
    @Delete({
        "delete from sys_dict",
        "where ID = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into sys_dict (CATEGORY, SUB_CATEGORY, ",
        "SORT_INDEX, NAME, ",
        "VALUE, P_CATEGORY, ",
        "VALID, NOTE)",
        "values (#{category,jdbcType=VARCHAR}, #{subCategory,jdbcType=VARCHAR}, ",
        "#{sortIndex,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, ",
        "#{value,jdbcType=VARCHAR}, #{pCategory,jdbcType=VARCHAR}, ",
        "#{valid,jdbcType=INTEGER}, #{note,jdbcType=VARCHAR})"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insert(SysDict record);

    @InsertProvider(type=SysDictSqlProvider.class, method="insertSelective")
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insertSelective(SysDict record);

    @Select({
        "select",
        "ID, CATEGORY, SUB_CATEGORY, SORT_INDEX, NAME, VALUE, P_CATEGORY, VALID, NOTE",
        "from sys_dict",
        "where ID = #{id,jdbcType=BIGINT}"
    })
    @Results({
        @Result(column="ID", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="CATEGORY", property="category", jdbcType=JdbcType.VARCHAR),
        @Result(column="SUB_CATEGORY", property="subCategory", jdbcType=JdbcType.VARCHAR),
        @Result(column="SORT_INDEX", property="sortIndex", jdbcType=JdbcType.INTEGER),
        @Result(column="NAME", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="VALUE", property="value", jdbcType=JdbcType.VARCHAR),
        @Result(column="P_CATEGORY", property="pCategory", jdbcType=JdbcType.VARCHAR),
        @Result(column="VALID", property="valid", jdbcType=JdbcType.INTEGER),
        @Result(column="NOTE", property="note", jdbcType=JdbcType.VARCHAR)
    })
    SysDict selectByPrimaryKey(Long id);
    
    /**
     * 根据类别查询字典表数据
     * 
     * @param category 类别名称
     * @return 
     */
    @Select({
        "select * from sys_dict",
        "where CATEGORY=#{category}",
        "order by SORT_INDEX"
    })
    List<SysDict> selectListByCategory(String category);

    @UpdateProvider(type=SysDictSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(SysDict record);

    @Update({
        "update sys_dict",
        "set CATEGORY = #{category,jdbcType=VARCHAR},",
          "SUB_CATEGORY = #{subCategory,jdbcType=VARCHAR},",
          "SORT_INDEX = #{sortIndex,jdbcType=INTEGER},",
          "NAME = #{name,jdbcType=VARCHAR},",
          "VALUE = #{value,jdbcType=VARCHAR},",
          "P_CATEGORY = #{pCategory,jdbcType=VARCHAR},",
          "VALID = #{valid,jdbcType=INTEGER},",
          "NOTE = #{note,jdbcType=VARCHAR}",
        "where ID = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(SysDict record);
}